package cn.edu.nju.software.bean.show;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import cn.edu.nju.software.bean.storage.Book;

public class BookShow {

	private int bookID;
	private String isbn;
	private String title;
	private String author;
	private String publisher;
	private CategoryShow category;
	private String abstractInfo;
	private BigDecimal price;
	private int sales;
	private String availableDate;
	private int stock;
	private String picPath;
	private String attachPath;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
	
	public BookShow(String title) {
		this.title = title;
	}

	public BookShow(Book book, CategoryShow category) {
		if (book != null) {
			this.bookID = book.getBookID();
			this.isbn = book.getIsbn();
			this.title = book.getTitle();
			this.author = book.getTitle();
			this.publisher = book.getPublisher();
			this.abstractInfo = book.getAbstractInfo();
			this.price = book.getPrice();
			this.sales = book.getSales();
			if (book.getAvailableDate() != null) {
				this.availableDate = dateFormat.format(book.getAvailableDate());
			}
			this.stock = book.getStock();
			this.picPath = book.getPicPath();
			this.attachPath = book.getAttachPath();
		}
		this.category = category;
	}

	public int getBookID() {
		return bookID;
	}

	public void setBookID(int bookID) {
		this.bookID = bookID;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public CategoryShow getCategory() {
		return category;
	}

	public void setCategory(CategoryShow category) {
		this.category = category;
	}

	public String getAbstractInfo() {
		return abstractInfo;
	}

	public void setAbstractInfo(String abstractInfo) {
		this.abstractInfo = abstractInfo;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public int getSales() {
		return sales;
	}

	public void setSales(int sales) {
		this.sales = sales;
	}

	public String getAvailableDate() {
		return availableDate;
	}

	public void setAvailableDate(String availableDate) {
		this.availableDate = availableDate;
	}
	
	public void setAvailableDate(Timestamp availableDate) {
		if (availableDate != null) {
			this.availableDate = dateFormat.format(availableDate);
		}
	}

	public int getStock() {
		return stock;
	}

	public void setStock(int stock) {
		this.stock = stock;
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	public String getAttachPath() {
		return attachPath;
	}

	public void setAttachPath(String attachPath) {
		this.attachPath = attachPath;
	}
	
	public Book getBook() {
		try {
			Timestamp availableDate = null;
			if (this.availableDate != null) {
				availableDate = new Timestamp(dateFormat.parse(this.availableDate).getTime());
			}
			return new Book(bookID, isbn, title, author, publisher, category.getCategoryID(), 
					abstractInfo, price, sales, availableDate, stock, picPath, attachPath);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
